export default function(config = {}) {
  const initialState = Object.assign({
    loading: false,
    data: [],
    hasMore: true,
    message: '',
    params: {},
    state: ''
  }, config.initialState)

  return function(state, action) {
    switch (action.type) {
      case `REQUEST_${config.state}`:
        return Object.assign({}, state, { loading: true, data: state.data })
      case `RECIEVE_${config.state}`: {
        const { limit, more } = action.params
        return Object.assign({}, state, {
          loading: false,
          data: more ? state.data.concat(action.data) : action.data,
          hasMore: action.data.length < limit ? false : true,
          message: action.message,
          params: action.params,
          state: config.state
        })
      }
      default:
        return state || initialState
    }
  }
}
